package com.cxyzy.demo.db.test

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.cxyzy.demo.db.AppDatabase
import com.cxyzy.demo.db.bean.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope

class DbTestDataInit(
    context: Context,
    workerParams: WorkerParameters
) : CoroutineWorker(context, workerParams) {

    override val coroutineContext = Dispatchers.IO

    override suspend fun doWork(): Result = coroutineScope {

        val database = AppDatabase.getInstance(applicationContext)

        val mList = mutableListOf<User>()

        (1..1000).forEach {
            mList.add(User(it, "user$it"))
        }
        database.userDao().add(mList)
        Result.success()
    }
}